﻿using Crawler.DBEntities;
using Crawler.WebEntities;
using System;
using System.Collections.Generic;
using System.Text;

namespace Crawler.DataHelper
{
    public static class SeedHelper
    {
        /// <summary>
        /// 判断种子是否过期
        /// </summary>
        /// <param name="uri"></param>
        /// <returns></returns>
        public static bool IsSeedTimeout(MyUri uri)
        {
            MysqlHelper helper = new MysqlHelper();
            return helper.IsSeedTimeOut(uri);
        }
        /// <summary>
        /// 获取种子的信息
        /// </summary>
        /// <param name="seed"></param>
        /// <param name="uri"></param>
        public static void GetSeedInfo(ref Seed seed,MyUri uri)
        {
            seed.LastUpdateTime = DateTime.Now;
            seed.CreateTime = DateTime.Now;
            //seed.Name = "正则表达式与title相同";
            seed.Url = uri.AbsoluteUri;
        }
        /// <summary>
        /// 更新种子的信息
        /// </summary>
        /// <param name="seed"></param>
        /// <param name="uri"></param>
        /// <returns></returns>
        public static bool UpdateToDB_seed(Seed seed,ref MyUri uri)
        {
            MysqlHelper helper = new MysqlHelper();
            return helper.UpdateSeed(seed, ref uri);
        }
        /// <summary>
        /// 插入种子记录
        /// </summary>
        /// <param name="seed"></param>
        /// <param name="uri"></param>
        /// <returns></returns>
        public static bool InsertSeed(Seed seed ,ref MyUri uri)
        {
            MysqlHelper helper = new MysqlHelper();
            return helper.InsertSeed(seed,ref uri);
        }
        public static List<MyUri> GetForumUries(MyUri uri)
        {
            MysqlHelper helper = new MysqlHelper ();
            return helper.SelectForums(uri);
        }
        public static bool IsSeedExist(MyUri uri)
        {
            bool isSeedExist = false;
            MysqlHelper helper = new MysqlHelper ();
            var seeds = helper.GetAllSeeds();
            foreach (var seed in seeds)
            {
                if (uri.AbsoluteUri.ToString().Equals(seed.Url))
                {
                    isSeedExist = true;
                    break;
                }
            }
            return isSeedExist;
        }
    }
}
